home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Tools - Objects / MacApp / MacApp 3.0a2 / Libraries / UAppleEvents.cp < prev    next >
Encoding:
Text File  |  1991-05-01  |  17.2 KB  |  637 lines  |  [TEXT/MPS ]

  1. // UAppleEvents.cp
  2. // Copyright © 1988-1991 by Apple Computer Inc.  All rights reserved.
  3.  
  4.  
  5. #ifndef __UAPPLEEVENTS__
  6. #include <UAppleEvents.h>
  7. #endif
  8.  
  9. #ifndef __RESOURCES__
  10. #include <Resources.h>
  11. #endif
  12.  
  13. #ifndef __UFAILURE__
  14. #include <UFailure.h>
  15. #endif
  16.  
  17. #ifndef __UMACAPPUTILITIES__
  18. #include <UMacAppUtilities.h>
  19. #endif
  20.  
  21. #ifndef __UAPPLICATION__
  22. #include <UApplication.h>
  23. #endif
  24.  
  25. #ifndef __UIterator__
  26. #include <UIterator.h>
  27. #endif
  28.  
  29. struct MAEventTableRec
  30. {
  31.     OSType theClass;
  32.     OSType theID;
  33.     long theValue;
  34. };
  35.  
  36.  
  37. typedef MAEventTableRec* MAEventTablePointer;
  38.  
  39.  
  40. //--------------------------------------------------------------------------------------------------
  41. #pragma segment AERes
  42.  
  43. pascal OSErr AppleEventsDispatch(const AppleEvent& message,
  44.                                  const AppleEvent& reply,
  45.                                  long info)
  46. {
  47.     // Dispatch through a method so that the behavior can be changed 
  48.     if (gApplication != NULL)
  49.         return gApplication->DispatchAppleEvent(message, reply, info);
  50. }
  51.  
  52. //--------------------------------------------------------------------------------------------------
  53. #pragma segment MAInit
  54.  
  55. pascal void InitUAppleEvents(void)
  56. {
  57.     if (qNeedsAppleEventMgr || gConfiguration.hasAppleEventMgr)
  58.     {
  59.         FailInfo fi;
  60.         short numberOfTables;
  61.         Size tableSize;
  62.         short tableElements;
  63.         Handle tableHandle = NULL;
  64.         MAEventTablePointer tablePtr;
  65.         short eventIndex;
  66.         SignedByte savedState;
  67.         
  68.         VOLATILE(tableHandle);
  69.  
  70.         // Look for all AppleEvent dispatch tables… 
  71.         numberOfTables = Count1Resources('aedt');// Count the number of 'aedt' resources 
  72.         FailResError();
  73.  
  74.         if (fi.Try())
  75.         {
  76.             for (short tableIndex = 1; tableIndex <= numberOfTables; ++tableIndex)
  77.             {
  78.                 tableHandle = Get1IndResource('aedt', tableIndex);
  79.                 FailResError();
  80.  
  81.                 savedState = LockHandleHigh(tableHandle);
  82.  
  83.                 tableSize = GetHandleSize(tableHandle);
  84.                 FailMemError();
  85.  
  86.                 tableElements = (short)(tableSize / sizeof(MAEventTableRec));
  87.  
  88.                 tablePtr = (MAEventTablePointer) * tableHandle;
  89.  
  90.                 for (eventIndex = 1; eventIndex <= tableElements; ++eventIndex)
  91.                 {
  92.                     // Install a single event handler for all events 
  93.                     FailOSErr(AEInstallEventHandler(tablePtr->theClass, tablePtr->theID, (EventHandlerProcPtr) & AppleEventsDispatch, tablePtr->theValue, FALSE));
  94.                     ++tablePtr;
  95.                 }
  96.  
  97.                 HSetState(tableHandle, savedState);
  98.                 ReleaseResource(tableHandle);
  99.             }
  100.             fi.Success();
  101.         }
  102.         else    // Recover
  103.         {
  104.             if (tableHandle)
  105.                 ReleaseResource(tableHandle);
  106.             fi.ReSignal();
  107.         }
  108.     }
  109. }
  110.  
  111. //--------------------------------------------------------------------------------------------------
  112. #pragma segment AEInit
  113.  
  114. pascal void TAppleEvent::IAppleEvent(const AEEventClass itsEventClass,
  115.                                      const AEEventID itsEventID,
  116.                                      const AEAddressDesc& itsAddress,
  117.                                      long itsSendingMode)
  118. {
  119.     FailInfo fi;
  120.     AppleEvent theMessage;
  121.  
  122.     fMessage.dataHandle = NULL;
  123.  
  124.     this->IObject();
  125.  
  126.     if (fi.Try())
  127.     {
  128.         FailOSErr(AECreateAppleEvent(itsEventClass, itsEventID, itsAddress, kAutoGenerateReturnID, kAnyTransactionID, theMessage));
  129.         fi.Success();
  130.     }
  131.     else    // Recover
  132.     {
  133.         this->Free();
  134.         fi.ReSignal();
  135.     }
  136.  
  137.     fMessage = theMessage;
  138.     fSendingMode = itsSendingMode;
  139.     fPriority = kAENormalPriority;
  140.     fTimeoutVal = kAEDefaultTimeout;
  141.     fFreeMessage = TRUE;                        // We created it, we need to free it
  142. }
  143.  
  144. //--------------------------------------------------------------------------------------------------
  145. #pragma segment AEInit
  146.  
  147. pascal void TAppleEvent::InitFromMessage(const AppleEvent& theMessage)
  148. {
  149.     this->IObject();
  150.  
  151.     fMessage = theMessage;
  152.     fSendingMode = kAENoReply;
  153.     fPriority = kAENormalPriority;
  154.     fTimeoutVal = kAEDefaultTimeout;
  155.     fFreeMessage = FALSE;                        // We don't really own the message so let the
  156.                                                 // AppleEvent manager be responsible
  157. }
  158.  
  159. //--------------------------------------------------------------------------------------------------
  160. #pragma segment AERes
  161.  
  162. pascal void TAppleEvent::Free(void)                // override 
  163. {
  164.     if (fFreeMessage)
  165.         AEDisposeDesc(fMessage);
  166.  
  167.     inherited::Free();
  168. }
  169.  
  170. //--------------------------------------------------------------------------------------------------
  171. #pragma segment AERes
  172.  
  173. pascal void TAppleEvent::Send(void)
  174. {
  175.     AppleEvent theMessage = fMessage;
  176.     AppleEvent theReply;
  177.  
  178.     FailOSErr(AESend(theMessage, theReply, fSendingMode, fPriority, fTimeoutVal, NULL, NULL));
  179.     // !!! We could use a good default cursor spinner here. 
  180. }
  181.  
  182. //--------------------------------------------------------------------------------------------------
  183. #pragma segment AERes
  184.  
  185. pascal void TAppleEvent::GetAddress(AEAddressDesc& theAddress)
  186.  
  187. {
  188.     AppleEvent theMessage = fMessage;
  189.  
  190.     FailOSErr(AEGetAttributeDesc(theMessage, keyAddressAttr, typeTargetID, theAddress));
  191. }
  192.  
  193. //--------------------------------------------------------------------------------------------------
  194. #pragma segment AERes
  195.  
  196. pascal short TAppleEvent::GetPriority(void)
  197. {
  198.     return fPriority;
  199. }
  200.  
  201. //--------------------------------------------------------------------------------------------------
  202. #pragma segment AERes
  203.  
  204. pascal short TAppleEvent::GetReturnID(void)
  205. {
  206.     AppleEvent theMessage = fMessage;
  207.     DescType actualType;
  208.     short returnID;
  209.     long actualSize;
  210.  
  211.     FailOSErr(AEGetAttributePtr(theMessage, keyReturnIDAttr, typeShortInteger, actualType, (Ptr) & returnID, sizeof(short), actualSize));
  212.     return returnID;
  213. }
  214.  
  215. //--------------------------------------------------------------------------------------------------
  216. #pragma segment AERes
  217.  
  218. pascal long TAppleEvent::GetTimeoutVal(void)
  219. {
  220.     return fTimeoutVal;
  221. }
  222.  
  223. //--------------------------------------------------------------------------------------------------
  224. #pragma segment AERes
  225.  
  226. pascal long TAppleEvent::GetTransactionID(void)
  227. {
  228.     AppleEvent theMessage = fMessage;
  229.     long transactionID;
  230.     DescType actualType;
  231.     long actualSize;
  232.  
  233.     FailOSErr(AEGetAttributePtr(theMessage, keyTransactionIDAttr, typeLongInteger, actualType, (Ptr) & transactionID, sizeof(long), actualSize));
  234.     return transactionID;
  235. }
  236.  
  237. //--------------------------------------------------------------------------------------------------
  238. #pragma segment AERes
  239.  
  240. pascal void TAppleEvent::SetAddress(const AEAddressDesc& theAddress)
  241. {
  242.     AppleEvent theMessage = fMessage;
  243.  
  244.     FailOSErr(AEPutAttributeDesc(theMessage, keyAddressAttr, theAddress));
  245. }
  246.  
  247. //--------------------------------------------------------------------------------------------------
  248. #pragma segment AERes
  249.  
  250. pascal void TAppleEvent::SetPriority(short thePriority)
  251. {
  252.     fPriority = thePriority;
  253. }
  254.  
  255. //--------------------------------------------------------------------------------------------------
  256. #pragma segment AERes
  257.  
  258. pascal void TAppleEvent::SetReturnID(short returnID)
  259. {
  260.     AppleEvent theMessage = fMessage;
  261.  
  262.     FailOSErr(AEPutAttributePtr(theMessage, keyReturnIDAttr, typeShortInteger, (Ptr) & returnID, sizeof(short)));
  263. }
  264.  
  265. //--------------------------------------------------------------------------------------------------
  266. #pragma segment AERes
  267.  
  268. pascal void TAppleEvent::SetTimeoutVal(long theTimeoutVal)
  269. {
  270.     fTimeoutVal = theTimeoutVal;
  271. }
  272.  
  273. //--------------------------------------------------------------------------------------------------
  274. #pragma segment AERes
  275.  
  276. pascal void TAppleEvent::SetTransactionID(long transactionID)
  277. {
  278.     AppleEvent theMessage = fMessage;
  279.  
  280.     FailOSErr(AEPutAttributePtr(theMessage, keyTransactionIDAttr, typeLongInteger, (Ptr) & transactionID, sizeof(long)));
  281. }
  282.  
  283. //--------------------------------------------------------------------------------------------------
  284. #pragma segment AERes
  285.  
  286. pascal void TAppleEvent::ReadInteger(const AEKeyword theKey,
  287.                                      short& theData)
  288. {
  289.     AppleEvent theMessage = fMessage;
  290.     DescType actualType;
  291.     long actualSize;
  292.     OSErr theErr;
  293.  
  294.  
  295.     theErr = AEGetParamPtr(theMessage, theKey, typeShortInteger, actualType, (Ptr) & theData, sizeof(short), actualSize);
  296.  
  297.     if ((theErr != noErr) && (theErr != errAEDescNotFound))
  298.         FailOSErr(theErr);
  299. }
  300.  
  301. //--------------------------------------------------------------------------------------------------
  302. #pragma segment AERes
  303.  
  304. pascal void TAppleEvent::ReadLong(const AEKeyword theKey,
  305.                                   long& theData)
  306. {
  307.     AppleEvent theMessage = fMessage;
  308.     DescType actualType;
  309.     long actualSize;
  310.     OSErr theErr;
  311.  
  312.  
  313.     theErr = AEGetParamPtr(theMessage, theKey, typeLongInteger, actualType, (Ptr) & theData, sizeof(long), actualSize);
  314.  
  315.     if ((theErr != noErr) && (theErr != errAEDescNotFound))
  316.         FailOSErr(theErr);
  317. }
  318.  
  319. //--------------------------------------------------------------------------------------------------
  320. #pragma segment AERes
  321.  
  322. pascal void TAppleEvent::ReadString(const AEKeyword theKey,
  323.                                       Str255& theData)
  324. {
  325.     AppleEvent theMessage = fMessage;
  326.     DescType actualType;
  327.     long actualSize;
  328.     OSErr theErr;
  329.  
  330.     theErr = AEGetParamPtr(theMessage, theKey, typeChar, actualType, (Ptr)&theData, sizeof(theData), actualSize);
  331.  
  332.     if ((theErr != noErr) && (theErr != errAEDescNotFound))
  333.         FailOSErr(theErr);
  334. }
  335.  
  336. //--------------------------------------------------------------------------------------------------
  337. #pragma segment AERes
  338.  
  339. pascal void TAppleEvent::ReadSectionHandle(const AEKeyword theKey,
  340.                                            SectionHandle& theData)
  341. {
  342.     AppleEvent theMessage = fMessage;
  343.     DescType actualType;
  344.     long actualSize;
  345.     OSErr theErr;
  346.  
  347.  
  348.     theErr = AEGetParamPtr(theMessage, theKey, typeSectionH, actualType, (Ptr) & theData, sizeof(SectionHandle), actualSize);
  349.  
  350.     if ((theErr != noErr) && (theErr != errAEDescNotFound))
  351.         FailOSErr(theErr);
  352. }
  353.  
  354. //--------------------------------------------------------------------------------------------------
  355. #pragma segment AERes
  356.  
  357. pascal void TAppleEvent::ReadPtrList(const AEKeyword theKey,
  358.                                      const DescType theType,
  359.                                      TDynamicArray* theData)
  360. {
  361.     FailInfo fi;
  362.     Ptr thePtr = NULL;
  363.     
  364.     VOLATILE(thePtr);
  365.  
  366.     if (fi.Try())
  367.     {
  368.         AppleEvent theMessage = fMessage;
  369.         AEDescList theDescList;
  370.         OSErr theErr;
  371.  
  372.         theErr = AEGetParamDesc(theMessage, theKey, typeAEList, theDescList);
  373.         if (theErr == noErr)
  374.         {
  375.             long items;
  376.             AEKeyword theActualKey;
  377.             DescType theActualType;
  378.             long theActualSize;
  379.  
  380.             FailOSErr(AECountItems(theDescList, items));// Get the number of elements in the list 
  381.             thePtr = NewPtr(theData->fElementSize);
  382.             FailMemError();
  383.  
  384.             for (short index = 1; index <= items; ++index)
  385.             {
  386.                 FailOSErr(AEGetNthPtr(theDescList, index, theType, theActualKey, theActualType, thePtr, theData->fElementSize, theActualSize));
  387.  
  388.                 // !!! We should probably check that theActualSize matches theMaxSize.  However it would 
  389.                 // really be an error only if the data type is not a string.  Currently this is an 
  390.                 // unresolved issue with the AppleEvent manager.  It may in the future return an error 
  391.  
  392.                 theData->InsertElementsBefore(theData->GetSize() + 1, thePtr, 1);
  393.             }
  394.         }
  395.         else if (theErr != errAEDescNotFound)
  396.             FailOSErr(theErr);
  397.         fi.Success();
  398.     }
  399.     else    // Recover
  400.     {
  401.         thePtr = DisposeIfPtr(thePtr);
  402.         fi.ReSignal();
  403.     }
  404.     thePtr = DisposeIfPtr(thePtr);
  405. }
  406.  
  407. //--------------------------------------------------------------------------------------------------
  408. #pragma segment AERes
  409.  
  410. pascal void TAppleEvent::ReadHandleList(const AEKeyword theKey,
  411.                                         const DescType theType,
  412.                                         TDynamicArray* theData)
  413. {
  414.     AppleEvent theMessage = fMessage;
  415.     AEDescList theDescList;
  416.     OSErr theErr;
  417.  
  418.     theErr = AEGetParamDesc(theMessage, theKey, typeAEList, theDescList);
  419.     if (theErr == noErr)
  420.     {
  421.         AEDesc theHandle;
  422.         AEKeyword theActualKey;
  423.         long items;
  424.  
  425.         theHandle.dataHandle = NULL;
  426.         FailOSErr(AECountItems(theDescList, items));// Get the number of elements in the list 
  427.         for (short index = 1; index <= items; ++index)
  428.         {
  429.             FailOSErr(AEGetNthDesc(theDescList, index, theType, theActualKey, theHandle));
  430.             theData->InsertElementsBefore(theData->GetSize() + 1, (Ptr) & theHandle.dataHandle, 1);
  431.         }
  432.     }
  433.     else if (theErr != errAEDescNotFound)
  434.         FailOSErr(theErr);
  435. }
  436.  
  437. //--------------------------------------------------------------------------------------------------
  438. #pragma segment AERes
  439.  
  440. pascal void TAppleEvent::ReadParameter(const AEKeyword theKey,
  441.                                        const DescType desiredType,
  442.                                        AEDesc& theData)
  443. {
  444.     AppleEvent theMessage = fMessage;
  445.  
  446.     FailOSErr(AEGetParamDesc(theMessage, theKey, desiredType, theData));
  447. }
  448.  
  449. //--------------------------------------------------------------------------------------------------
  450. #pragma segment AERes
  451.  
  452. pascal void TAppleEvent::ReadParameterPtr(const AEKeyword theKey,
  453.                                           const DescType desiredType,
  454.                                           DescType actualType,
  455.                                           Ptr theData,
  456.                                           long maximumSize,
  457.                                           long& actualSize)
  458. {
  459.     AppleEvent theMessage = fMessage;
  460.  
  461.     FailOSErr(AEGetParamPtr(theMessage, theKey, desiredType, actualType, theData, maximumSize, actualSize));
  462. }
  463.  
  464. //--------------------------------------------------------------------------------------------------
  465. #pragma segment AERes
  466.  
  467. pascal void TAppleEvent::WriteInteger(const AEKeyword theKey,
  468.                                       short theData)
  469. {
  470.     AppleEvent theMessage = fMessage;
  471.  
  472.     FailOSErr(AEPutParamPtr(theMessage, theKey, typeShortInteger, (Ptr) & theData, sizeof(short)));
  473. }
  474.  
  475. //--------------------------------------------------------------------------------------------------
  476. #pragma segment AERes
  477.  
  478. pascal void TAppleEvent::WriteLong(const AEKeyword theKey,
  479.                                    long theData)
  480. {
  481.     AppleEvent theMessage = fMessage;
  482.  
  483.     FailOSErr(AEPutParamPtr(theMessage, theKey, typeLongInteger, (Ptr) & theData, sizeof(long)));
  484. }
  485.  
  486. //--------------------------------------------------------------------------------------------------
  487. #pragma segment AERes
  488.  
  489. pascal void TAppleEvent::WriteString(const AEKeyword theKey,
  490.                                        const Str255& theData)
  491. {
  492.     AppleEvent theMessage = fMessage;
  493.     Str255 localString = theData;
  494.  
  495.     FailOSErr(AEPutParamPtr(theMessage, theKey, typeChar, (Ptr) &localString, sizeof(localString)));
  496. }
  497.  
  498. //--------------------------------------------------------------------------------------------------
  499. #pragma segment AERes
  500.  
  501. pascal void TAppleEvent::WriteSectionHandle(const AEKeyword theKey,
  502.                                             SectionHandle theData)
  503. {
  504.     AppleEvent theMessage = fMessage;
  505.  
  506.     FailOSErr(AEPutParamPtr(theMessage, theKey, typeSectionH, (Ptr) & theData, sizeof(SectionHandle)));
  507. }
  508.  
  509. //--------------------------------------------------------------------------------------------------
  510. #pragma segment AERes
  511.  
  512. pascal void TAppleEvent::WritePtrList(const AEKeyword theKey,
  513.                                       const DescType theType,
  514.                                       TDynamicArray* theData)
  515. {
  516.     FailInfo fi;
  517.     AEDescList theDescList;
  518.  
  519.     theDescList.dataHandle = NULL;
  520.     if (fi.Try())
  521.     {
  522.         AppleEvent theMessage = fMessage;
  523.  
  524.         FailOSErr(AECreateList(NULL, 0, FALSE, theDescList));
  525.  
  526.         {
  527.             // Create a block for the iterator to disambiguate it from the failure handler it's nested in
  528.             CArrayIterator iter(theData);
  529.             
  530.             for (ArrayIndex i = iter.FirstIndex(); iter.More(); i = iter.NextIndex())
  531.                 FailOSErr(AEPutPtr(theDescList, (long)i, theType, theData->ComputeAddress(i), theData->fElementSize));
  532.         }
  533.         FailOSErr(AEPutParamDesc(theMessage, theKey, theDescList));
  534.         fi.Success();
  535.     }
  536.     else    // Recover
  537.     {
  538.         FailOSErr(AEDisposeDesc(theDescList));
  539.         fi.ReSignal();
  540.     }
  541.  
  542.     FailOSErr(AEDisposeDesc(theDescList));
  543. }
  544.  
  545. //--------------------------------------------------------------------------------------------------
  546. #pragma segment AERes
  547.  
  548. pascal void TAppleEvent::WriteHandleList(const AEKeyword theKey,
  549.                                          const DescType theType,
  550.                                          TDynamicArray* theData)
  551. {
  552.     FailInfo fi;
  553.     AEDescList theDescList;
  554.  
  555.     theDescList.dataHandle = NULL;
  556.     if (fi.Try())
  557.     {
  558.         AppleEvent theMessage = fMessage;
  559.  
  560.         FailOSErr(AECreateList(NULL, 0, FALSE, theDescList));
  561.  
  562.         {
  563.             // Create a block for the iterator to disambiguate it from the failure handler it's nested in
  564.             CArrayIterator iter(theData);
  565.             
  566.             for (ArrayIndex i = iter.FirstIndex(); iter.More(); i = iter.NextIndex())
  567.             {
  568.                 AEDesc theElement;
  569.             
  570.                 theElement.dataHandle = NULL;
  571.                 theElement.descriptorType = theType;
  572.                 theElement.dataHandle = *((Handle *)(theData->ComputeAddress(i)));
  573.                 FailOSErr(AEPutDesc(theDescList, (long)i, theElement));
  574.             }
  575.         }
  576.  
  577.         FailOSErr(AEPutParamDesc(theMessage, theKey, theDescList));
  578.         fi.Success();
  579.     }
  580.     else    // Recover
  581.     {
  582.         FailOSErr(AEDisposeDesc(theDescList));
  583.         fi.ReSignal();
  584.     }
  585.  
  586.     FailOSErr(AEDisposeDesc(theDescList));
  587. }
  588.  
  589. //--------------------------------------------------------------------------------------------------
  590. #pragma segment AERes
  591.  
  592. pascal void TAppleEvent::WriteParameter(const AEKeyword theKey,
  593.                                         const AEDesc& theData)
  594. {
  595.     AppleEvent theMessage = fMessage;
  596.  
  597.     FailOSErr(AEPutParamDesc(theMessage, theKey, theData));
  598. }
  599.  
  600. //--------------------------------------------------------------------------------------------------
  601. #pragma segment AERes
  602.  
  603. pascal void TAppleEvent::WriteParameterPtr(const AEKeyword theKey,
  604.                                            const DescType typeCode,
  605.                                            Ptr theData,
  606.                                            long dataSize)
  607. {
  608.     AppleEvent theMessage = fMessage;
  609.  
  610.     FailOSErr(AEPutParamPtr(theMessage, theKey, typeCode, theData, dataSize));
  611. }
  612.  
  613. //--------------------------------------------------------------------------------------------------
  614. #pragma segment AERes
  615.  
  616. pascal void TAppleEvent::DeleteParameter(const AEKeyword theKey)
  617. {
  618.     AppleEvent theMessage = fMessage;
  619.  
  620.     FailOSErr(AEDeleteKeyDesc(theMessage, theKey));
  621. }
  622.  
  623. //--------------------------------------------------------------------------------------------------
  624. #pragma segment MAFields
  625.  
  626. pascal void TAppleEvent::Fields(TObject* obj)    // override 
  627. {
  628.     obj->DoToField("TAppleEvent", (Ptr)NULL, bClass);
  629.     obj->DoToField("fMessage", (Ptr) & fMessage, bHandle);
  630.     obj->DoToField("fSendingMode", (Ptr) & fSendingMode, bLongInt);
  631.     obj->DoToField("fPriority", (Ptr) & fPriority, bInteger);
  632.     obj->DoToField("fTimeoutVal", (Ptr) & fTimeoutVal, bLongInt);
  633.     inherited::Fields(obj);
  634. }
  635.  
  636.  
  637.